package com.dankegongyu.lib

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.multidex.MultiDex
import com.alibaba.android.arouter.launcher.ARouter
import com.dankegongyu.lib.common.util.*
import com.dankegongyu.lib.common.util.fresco.FrescoConfigConstants
import com.dankegongyu.lib.common.util.statusbar.DKStatusBarConfig
import com.dankegongyu.lib.common.util.statusbar.DKStatusBarMode
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * @author wupuquan
 * @version 1.0
 * @since 2018/4/3 13:24
 */
class CommonApp : Application() {
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(base)
    }

    @SuppressLint("MissingPermission")
    override fun onCreate() {
        super.onCreate()
        GlobalContext.setApplication(this)
        if (BuildConfig.DEBUG) {
            // 这两行必须写在init之前，否则这些配置在init过程中将无效
            ARouter.openLog() // 打印日志
            ARouter.openDebug() // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            ARouter.printStackTrace() // 打印日志的时候打印线程堆栈
        }
        ARouter.init(this) // 尽可能早，推荐在Application中初始化
        DKLog.setLoggingEnabled(BuildConfig.DEBUG)
        FrescoConfigConstants.initialize(this)

//        DKFileUtil.createAppFile("aaa.pdf");
//        DKFileUtil.createAppFile("Download", "aaa.pdf");
//        DKFileUtil.createAppDir("Download");
        DKLog.e("IMEI: " + PhoneUtil.getIMEI())
        DKLog.e("MAC地址：" + DeviceUtils.getMacAddress())
        getSerial()

        ApkUtils.scanLocalInstalledApps(packageManager)
        initStatusBarConfig()

        testCoroutine()

        executeFunc { testExecuteFun() }
        executeFuncAsync { testExecuteFun() }
    }

    /**
     * 状态栏配置
     */
    private fun initStatusBarConfig() {
        DKStatusBarConfig.getInstance()
                .setUseCustomStatusBar(true)
                .setStatusBarMode(DKStatusBarMode.LIGHT).statusBarColor = R.color.navigation_bg_color
    }

    private fun testCoroutine() {
        Log.e("Coroutine" + 0, " " + Thread.currentThread().name)
        for (i in 1..29) {
            GlobalScope.launch {
                Log.e("Coroutine" + i, " " + Thread.currentThread().name)
            }
        }
        GlobalScope.launch(Dispatchers.Main) {
            Log.e("Coroutine" + 110, " " + Thread.currentThread().name)
        }
    }

    private fun executeFunc(func: () -> Unit) {
        func()
    }

    private fun executeFuncAsync(func: () -> Unit) {
        GlobalScope.launch {
            func()
        }
    }

    private fun testExecuteFun() {
//        DKLog.e("testExecuteFun, currentThread: " + Thread.currentThread().name)
    }


    @SuppressLint("HardwareIds")
    private fun getSerial() {
        var serial = ""
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            serial = Build::class.java.getField("SERIAL")[null].toString()
        } else {
            serial = Build.SERIAL
        }
        DKLog.e("Serial: " + serial)
    }
}